/*
 * #%L
 * SwiftUIDataAccessLayer
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.linkwithweb.products.swiftui.context.SystemContext;
import com.linkwithweb.products.swiftui.dataobject.FrameworkApplication;
import com.linkwithweb.products.swiftui.dataobject.FrameworkTextReport;
import com.linkwithweb.products.swiftui.utils.FreeMarkerEngine;

/**
 * @author ashwin kumar
 * 
 */
public abstract class AbstractServiceImpl {

	private List<String> systemColumnNames = new ArrayList<String>();

	/**
	 * @param paramName
	 * @return
	 */
	protected String getRequestParamter(String paramName) {
		String paramString = "${GlobalContext.requestContext.requestWrapper." + paramName + "}";
		try {
			return FreeMarkerEngine.getInstance().evaluateString(paramString);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @param paramName
	 * @return
	 */
	protected String getRequestParamterWithNoEvaluation(String paramName) {
		return SystemContext.getInstance().getRequestContext().getRequestWrapper().getParameter(paramName);
	}

	/**
	 * @param paramConditions
	 * @param insertSQL
	 */
	protected void insertSystemParamValues(List paramConditions, StringBuffer insertSQL) {
		FrameworkApplication app = SystemContext.getInstance().getUserContext().getFrameworkUser()
				.getListOfApplications().get(0);
		String userRole = SystemContext.getInstance().getUserContext().getRole();

		paramConditions.add("1");
		paramConditions.add(app.getAppId());
		paramConditions.add(userRole);
		paramConditions.add(SystemContext.getInstance().getUserContext().getFrameworkUser().getUniqueKey());
		paramConditions.add(Calendar.getInstance().getTime());
		paramConditions.add(SystemContext.getInstance().getUserContext().getFrameworkUser().getUniqueKey());
		paramConditions.add(Calendar.getInstance().getTime());

		insertSQL.append("?,");
		insertSQL.append("?,");
		insertSQL.append("?,");
		insertSQL.append("?,");
		insertSQL.append("?,");
		insertSQL.append("?,");
		insertSQL.append("?");
	}

	/**
	 * @param insertSQL
	 */
	protected void insertSystemParams(StringBuffer insertSQL) {
		insertSQL.append("IS_ACTIVE,IDENTIFIER,IDENTIFIER_ROLE,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE");
	}

	/**
	 * @param columnName
	 * @return
	 */
	protected boolean isSystemColumn(String columnName) {
		if (systemColumnNames.isEmpty()) {
			systemColumnNames.add("IS_ACTIVE");
			systemColumnNames.add("IDENTIFIER");
			systemColumnNames.add("IDENTIFIER_ROLE");
			systemColumnNames.add("CREATED_BY");
			systemColumnNames.add("CREATED_DATE");
			systemColumnNames.add("MODIFIED_BY");
			systemColumnNames.add("MODIFIED_DATE");
		}
		for (String sysColumnName : systemColumnNames) {
			if (StringUtils.equalsIgnoreCase(columnName, sysColumnName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param reportComponent
	 * @return
	 */
	protected String getDataSourceName(FrameworkTextReport reportComponent) {
		/**
		 * Now define which datasource to call
		 */
		String dataSourceName = "frameworkJdbcTemplate";

		if (!(StringUtils.containsIgnoreCase(reportComponent.getFrameworkTable().getTableName(), "FRM_") && StringUtils
				.containsIgnoreCase(reportComponent.getFrameworkTable().getTableSchema(), "AUTOMATA"))) {
			dataSourceName = SystemContext.getInstance().getUserContext().getFrameworkUser().getListOfApplications()
					.get(0).getAppId();
		}
		return dataSourceName;
	}

	/**
	 * @param tableName
	 * @param schemaName
	 * @return
	 */
	protected String getDatasourceName(String tableName, String schemaName) {
		String dataSourceName = "frameworkJdbcTemplate";

		if (!(StringUtils.containsIgnoreCase(tableName, "FRM_") && StringUtils.containsIgnoreCase(schemaName,
				"AUTOMATA"))) {
			dataSourceName = SystemContext.getInstance().getUserContext().getFrameworkUser().getListOfApplications()
					.get(0).getAppId();
		}
		return dataSourceName;
	}

	protected Date getDateObject(String dateString, String format) {
		Date returnDate = null;
		try {

			SimpleDateFormat formatter;
			formatter = new SimpleDateFormat(format);
			java.util.Calendar cal = Calendar.getInstance();
			formatter.setCalendar(cal);

			returnDate = formatter.parse(dateString);
			// System.out.println(formatter.format(newDate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}

		return returnDate;
	}

}
